home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Developers / src / out-of-phase-102-c / OutOfPhase 1.02 Source / OutOfPhase Folder / ExecuteSynthesis.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-23  |  17.6 KB  |  505 lines  |  [TEXT/KAHL]

  1. /* ExecuteSynthesis.c */
  2. /*****************************************************************************/
  3. /*                                                                           */
  4. /*    Out Of Phase:  Digital Music Synthesis on General Purpose Computers    */
  5. /*    Copyright (C) 1994  Thomas R. Lawrence                                 */
  6. /*                                                                           */
  7. /*    This program is free software; you can redistribute it and/or modify   */
  8. /*    it under the terms of the GNU General Public License as published by   */
  9. /*    the Free Software Foundation; either version 2 of the License, or      */
  10. /*    (at your option) any later version.                                    */
  11. /*                                                                           */
  12. /*    This program is distributed in the hope that it will be useful,        */
  13. /*    but WITHOUT ANY WARRANTY; without even the implied warranty of         */
  14. /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
  15. /*    GNU General Public License for more details.                           */
  16. /*                                                                           */
  17. /*    You should have received a copy of the GNU General Public License      */
  18. /*    along with this program; if not, write to the Free Software            */
  19. /*    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
  20. /*                                                                           */
  21. /*    Thomas R. Lawrence can be reached at tomlaw@world.std.com.             */
  22. /*                                                                           */
  23. /*****************************************************************************/
  24.  
  25. #include "MiscInfo.h"
  26. #include "Audit.h"
  27. #include "Debug.h"
  28. #include "Definitions.h"
  29.  
  30. #include "ExecuteSynthesis.h"
  31. #include "Memory.h"
  32. #include "MainWindowStuff.h"
  33. #include "LinearTransition.h"
  34. #include "PlayTrackInfoThang.h"
  35. #include "Array.h"
  36. #include "WaveTableOscControl.h"
  37. #include "EnvelopeState.h"
  38. #include "LFOGenerator.h"
  39. #include "InstrList.h"
  40. #include "InstrObject.h"
  41. #include "TrackObject.h"
  42. #include "SampleOscControl.h"
  43. #include "ModulationOscControl.h"
  44. #include "Fractions.h"
  45. #include "FrameObject.h"
  46. #include "DeterminedNoteStructure.h"
  47. #include "TempoController.h"
  48. #include "OscBankPlayer.h"
  49. #include "DataMunging.h"
  50. #include "Alert.h"
  51. #include "CheckNameUniqueness.h"
  52. #include "ErrorDaemon.h"
  53.  
  54.  
  55. typedef struct PlayListNodeRec
  56.     {
  57.         struct PlayListNodeRec*    Next;
  58.         PlayTrackInfoRec*                ThisTrack;
  59.     } PlayListNodeRec;
  60.  
  61.  
  62. /* build the list of objects involved in playing. */
  63. static SynthErrorCodes    BuildPlayList(PlayListNodeRec** ListOut,
  64.                                                 ArrayRec* TrackObjectList, MyBoolean InStereoFlag,
  65.                                                 MainWindowRec* MainWindow,
  66.                                                 LargeBCDType OverallVolumeScalingReciprocal,
  67.                                                 long SamplingRate, long EnvelopeRate, MyBoolean TimeInterp,
  68.                                                 MyBoolean WaveInterp, TempoControlRec* TempoControl,
  69.                                                 long ScanningGapWidthInEnvelopeTicks,
  70.                                                 ErrorDaemonRec* ErrorDaemon)
  71.     {
  72.         PlayListNodeRec*        TrackPlayList;
  73.         long                                Scan;
  74.         long                                Limit;
  75.         InstrListRec*                InstrList;
  76.         SynthErrorCodes            Error;
  77.  
  78.         /* build list of tracks */
  79.         CheckPtrExistence(MainWindow);
  80.         CheckPtrExistence(TrackObjectList);
  81.         InstrList = MainWindowGetInstrList(MainWindow);
  82.         TrackPlayList = NIL;
  83.         Limit = ArrayGetLength(TrackObjectList);
  84.         for (Scan = 0; Scan < Limit; Scan += 1)
  85.             {
  86.                 TrackObjectRec*            PossibleTrack;
  87.                 PlayListNodeRec*        NewListNode;
  88.                 PlayTrackInfoRec*        TrackPlayObject;
  89.                 InstrObjectRec*            BaseInstrument;
  90.                 char*                                InstrumentNameCopy;
  91.  
  92.                 /* get the track */
  93.                 PossibleTrack = (TrackObjectRec*)ArrayGetElement(TrackObjectList,Scan);
  94.                 CheckPtrExistence(PossibleTrack);
  95.                 /* get the instrument to be used */
  96.                 InstrumentNameCopy = TrackObjectGetInstrName(PossibleTrack);
  97.                 if (InstrumentNameCopy == NIL)
  98.                     {
  99.                         Error = eSynthNoMemory;
  100.                         goto TrackListBuildFailure1;
  101.                     }
  102.                 BaseInstrument = InstrListLookupNamedInstr(InstrList,InstrumentNameCopy);
  103.                 ReleasePtr(InstrumentNameCopy);
  104.                 if (BaseInstrument == NIL)
  105.                     {
  106.                         char*                                TrackName;
  107.                         MyBoolean                        MessageSuccess = False;
  108.  
  109.                         TrackName = TrackObjectGetNameCopy(PossibleTrack);
  110.                         if (TrackName != NIL)
  111.                             {
  112.                                 char*                                NullTerminated;
  113.  
  114.                                 NullTerminated = BlockToStringCopy(TrackName);
  115.                                 if (NullTerminated != NIL)
  116.                                     {
  117.                                         AlertHalt("The track '_' uses a nonexistent instrument.",
  118.                                             NullTerminated);
  119.                                         MessageSuccess = True;
  120.                                         ReleasePtr(NullTerminated);
  121.                                     }
  122.                                 ReleasePtr(TrackName);
  123.                             }
  124.                         if (!MessageSuccess)
  125.                             {
  126.                                 AlertHalt("A track uses a nonexistent instrument.",NIL);
  127.                             }
  128.                         Error = eSynthUndefinedInstrumentError;
  129.                         goto TrackListBuildFailure1;
  130.                     }
  131.                 /* build the track */
  132.                 TrackPlayObject = NewPlayTrackInfo(PossibleTrack,
  133.                     GetInstrObjectRawData(BaseInstrument),InStereoFlag,
  134.                     OverallVolumeScalingReciprocal,SamplingRate,EnvelopeRate,TimeInterp,
  135.                     WaveInterp,TempoControl,ScanningGapWidthInEnvelopeTicks,ErrorDaemon);
  136.                 if (TrackPlayObject == NIL)
  137.                     {
  138.                         Error = eSynthNoMemory;
  139.                      TrackListBuildFailure1:
  140.                         while (TrackPlayList != NIL)
  141.                             {
  142.                                 PlayListNodeRec*        Temp;
  143.  
  144.                                 DisposePlayTrackInfo(TrackPlayList->ThisTrack);
  145.                                 Temp = TrackPlayList;
  146.                                 TrackPlayList = TrackPlayList->Next;
  147.                                 ReleasePtr((char*)Temp);
  148.                             }
  149.                         return Error;
  150.                     }
  151.                 /* add this one to the list */
  152.                 NewListNode = (PlayListNodeRec*)AllocPtrCanFail(sizeof(PlayListNodeRec),
  153.                     "PlayListNodeRec");
  154.                 if (NewListNode == NIL)
  155.                     {
  156.                         Error = eSynthNoMemory;
  157.                      TrackListBuildFailure2:
  158.                         DisposePlayTrackInfo(TrackPlayObject);
  159.                         goto TrackListBuildFailure1;
  160.                     }
  161.                 NewListNode->ThisTrack = TrackPlayObject;
  162.                 NewListNode->Next = TrackPlayList;
  163.                 TrackPlayList = NewListNode;
  164.             }
  165.         *ListOut = TrackPlayList;
  166.         return eSynthDone;
  167.     }
  168.  
  169.  
  170. /* this routine scans through the key playlist and determines the exact point */
  171. /* at which playback should begin. */
  172. static void                        FindStartPoint(TrackObjectRec* KeyTrack, long FrameToStartAt,
  173.                                                 FractionRec* StartTimeOut)
  174.     {
  175.         long                                Scan;
  176.         FractionRec                    Counter;
  177.  
  178.         CheckPtrExistence(KeyTrack);
  179.         Counter.Integer = 0;
  180.         Counter.Fraction = 0;
  181.         Counter.Denominator = (64*3*5*7*2);
  182.         ERROR(FrameToStartAt > TrackObjectGetNumFrames(KeyTrack),PRERR(ForceAbort,
  183.             "FindStartPoint:  start frame is beyond end of track"));
  184.         for (Scan = 0; Scan < FrameToStartAt; Scan += 1)
  185.             {
  186.                 FrameObjectRec*            Frame;
  187.                 FractionRec                    TempDuration;
  188.  
  189.                 Frame = TrackObjectGetFrame(KeyTrack,Scan);
  190.                 CheckPtrExistence(Frame);
  191.                 DurationOfFrame(Frame,&TempDuration);
  192.                 AddFractions(&TempDuration,&Counter,&Counter);
  193.             }
  194.         *StartTimeOut = Counter;
  195.     }
  196.  
  197.  
  198. /* This routine does all of the work. */
  199. /* The DataOutCallback is called every time a block of data is */
  200. /* ready to be sent to the target device; this is provided so that data can be */
  201. /* redirected to a file or postprocessed in some way before playback. */
  202. /* the KeyTrack and FrameToStartAt provide a reference point indicating where */
  203. /* playback should occur.  if KeyTrack is NIL, then playback begins at the beginning. */
  204. /* the rate parameters are in operations per second. */
  205. SynthErrorCodes                Synthesizer(struct MainWindowRec* MainWindow,
  206.                                                 MyBoolean (*DataOutCallback)(void* Refcon,
  207.                                                     largefixedsigned* DataBlock, long NumFrames,
  208.                                                     MyBoolean* AbortPlaybackFlagOut),
  209.                                                 void* DataOutRefcon, struct ArrayRec* ListOfTracks,
  210.                                                 struct TrackObjectRec* KeyTrack, long FrameToStartAt,
  211.                                                 long SamplingRate, long EnvelopeRate, MyBoolean UseStereo,
  212.                                                 LargeBCDType DefaultBeatsPerMinute,
  213.                                                 LargeBCDType OverallVolumeScalingReciprocal,
  214.                                                 MyBoolean InterpOverTime, MyBoolean InterpAcrossWaves,
  215.                                                 LargeBCDType ScanningGap, ErrorDaemonRec* ErrorDaemon)
  216.     {
  217.         SynthErrorCodes            Error;
  218.         largefixedsigned*        SampleArray;
  219.         long                                SampleArrayLength;
  220.  
  221.  
  222.         /* error checking */
  223.         CheckPtrExistence(MainWindow);
  224.         CheckPtrExistence(ListOfTracks);
  225.         CheckPtrExistence(ErrorDaemon);
  226.         ERROR(DataOutCallback == NIL,PRERR(ForceAbort,"Synthesizer: bad DataOutCallback"));
  227.  
  228.         /* check to see that there aren't any naming ambiguities */
  229.         if (!CheckNameUniqueness(MainWindow))
  230.             {
  231.                 return eSynthDuplicateNames;
  232.             }
  233.  
  234.         /* make sure all objects are up to date */
  235.         if (MainWindowMakeEverythingUpToDate(MainWindow))
  236.             {
  237.                 PlayListNodeRec*        PlayTrackList;
  238.  
  239.                 SampleArrayLength = 1000;
  240.                 SampleArray = (largefixedsigned*)AllocPtrCanFail(SampleArrayLength
  241.                     * sizeof(largefixedsigned),"SampleArray");
  242.                 if (SampleArray != NIL)
  243.                     {
  244.                         TempoControlRec*        TempoControl;
  245.                         long                                ScanningGapWidthInEnvelopeTicks;
  246.  
  247.                         /* figure out how large the scanning gap is */
  248.                         ScanningGapWidthInEnvelopeTicks = LargeBCD2Double(ScanningGap) * EnvelopeRate;
  249.  
  250.                         TempoControl = NewTempoControl(DefaultBeatsPerMinute);
  251.                         if (TempoControl != NIL)
  252.                             {
  253.                                 Error = BuildPlayList(&PlayTrackList,ListOfTracks,UseStereo,MainWindow,
  254.                                     OverallVolumeScalingReciprocal,SamplingRate,EnvelopeRate,
  255.                                     InterpOverTime,InterpAcrossWaves,TempoControl,
  256.                                     ScanningGapWidthInEnvelopeTicks,ErrorDaemon);
  257.                                 if (Error == eSynthDone)
  258.                                     {
  259.                                         PlayListNodeRec*        Scan;
  260.                                         PlayListNodeRec*        Lag;
  261.                                         MyBoolean                        AbortPlaybackFlag;
  262.                                         FractionRec                    MomentOfStarting;
  263.                                         LargeBCDType                CurrentBeatsPerMinute;
  264.                                         long                                ScanningGapFrontInEnvelopeTicks;
  265.                                         double                            EnvelopeClockAccumulatorFraction;
  266.                                         double                            NoteDurationClockAccumulatorFraction;
  267.                                         double                            SamplesPerEnvelopeClock;
  268.                                         double                            DurationTicksPerEnvelopeClock;
  269.  
  270.                                         /* calculate the moment of starting for tracks */
  271.                                         FindStartPoint(KeyTrack,FrameToStartAt,&MomentOfStarting);
  272.                                         /* cue all tracks up to this point */
  273.                                         Scan = PlayTrackList;
  274.                                         while (Scan != NIL)
  275.                                             {
  276.                                                 if (!CuePlayTrackInfoToPoint(Scan->ThisTrack,&MomentOfStarting))
  277.                                                     {
  278.                                                         Error = eSynthNoMemory;
  279.                                                         goto PlaybackFailurePoint;
  280.                                                     }
  281.                                                 Scan = Scan->Next;
  282.                                             }
  283.  
  284.                                         /* this value is for determining when in REAL time (not score time) */
  285.                                         /* each note begins */
  286.                                         ScanningGapFrontInEnvelopeTicks = 0;
  287.  
  288.                                         /* initialize tempo thing */
  289.                                         CurrentBeatsPerMinute = DefaultBeatsPerMinute;
  290.  
  291.                                         /* initialize accumulators */
  292.                                         EnvelopeClockAccumulatorFraction = 0;
  293.                                         NoteDurationClockAccumulatorFraction = 0;
  294.                                         /* calculate increment factors */
  295.                                         SamplesPerEnvelopeClock = (double)SamplingRate / EnvelopeRate;
  296.                                         DurationTicksPerEnvelopeClock = ((LargeBCD2Double(DefaultBeatsPerMinute)
  297.                                             / (4/*beats per whole note*/ * 60/*seconds per minute*/))
  298.                                             / EnvelopeRate) * DURATIONUPDATECLOCKRESOLUTION;
  299.  
  300.                                         /* play */
  301.                                         AbortPlaybackFlag = False;
  302.                                         while ((PlayTrackList != NIL) && !AbortPlaybackFlag)
  303.                                             {
  304.                                                 long                                NumSampleFrames;
  305.                                                 long                                NumNoteDurationTicks;
  306.                                                 LargeBCDType                OldBeatsPerMinute;
  307.  
  308.                                                 /* figure out how many note duration ticks to generate */
  309.                                                 /* increment counter */
  310.                                                 NoteDurationClockAccumulatorFraction += DurationTicksPerEnvelopeClock;
  311.                                                 /* round down */
  312.                                                 NumNoteDurationTicks = (long)NoteDurationClockAccumulatorFraction;
  313.                                                 /* subtract off what we're taking out this time around, */
  314.                                                 /* leaving the extra little bit in there */
  315.                                                 NoteDurationClockAccumulatorFraction -= NumNoteDurationTicks;
  316.  
  317.                                                 /* figure out how many samples to do before the next envelope */
  318.                                                 if (ScanningGapFrontInEnvelopeTicks >= ScanningGapWidthInEnvelopeTicks)
  319.                                                     {
  320.                                                         /* we're really sampling stuff */
  321.                                                         EnvelopeClockAccumulatorFraction += SamplesPerEnvelopeClock;
  322.                                                         NumSampleFrames = (long)EnvelopeClockAccumulatorFraction;
  323.                                                         EnvelopeClockAccumulatorFraction -= NumSampleFrames;
  324.                                                     }
  325.                                                  else
  326.                                                     {
  327.                                                         /* scanning gap is still opening, so we're not sampling */
  328.                                                         NumSampleFrames = 0;
  329.                                                     }
  330.  
  331.                                                 /* see if we need to resize the output workspace array */
  332.                                                 if (UseStereo)
  333.                                                     {
  334.                                                         long                                Scan;
  335.  
  336.                                                         /* stereo array sizing */
  337.                                                         if (NumSampleFrames * 2 > SampleArrayLength)
  338.                                                             {
  339.                                                                 largefixedsigned*        TempArray;
  340.  
  341.                                                                 TempArray = (largefixedsigned*)ResizePtr(
  342.                                                                     (char*)SampleArray,NumSampleFrames * 2
  343.                                                                     * sizeof(largefixedsigned));
  344.                                                                 if (TempArray == NIL)
  345.                                                                     {
  346.                                                                         Error = eSynthNoMemory;
  347.                                                                         goto PlaybackFailurePoint;
  348.                                                                     }
  349.                                                                 SampleArray = TempArray;
  350.                                                                 SampleArrayLength = NumSampleFrames * 2;
  351.                                                             }
  352.                                                         /* initialize the array */
  353.                                                         for (Scan = 0; Scan < NumSampleFrames * 2; Scan += 1)
  354.                                                             {
  355.                                                                 PRNGCHK(SampleArray,&(SampleArray[Scan]),
  356.                                                                     sizeof(SampleArray[Scan]));
  357.                                                                 SampleArray[Scan] = 0;
  358.                                                             }
  359.                                                     }
  360.                                                  else
  361.                                                     {
  362.                                                         long                                Scan;
  363.  
  364.                                                         /* mono array sizing */
  365.                                                         if (NumSampleFrames > SampleArrayLength)
  366.                                                             {
  367.                                                                 largefixedsigned*        TempArray;
  368.  
  369.                                                                 TempArray = (largefixedsigned*)ResizePtr(
  370.                                                                     (char*)SampleArray,NumSampleFrames
  371.                                                                     * sizeof(largefixedsigned));
  372.                                                                 if (TempArray == NIL)
  373.                                                                     {
  374.                                                                         Error = eSynthNoMemory;
  375.                                                                         goto PlaybackFailurePoint;
  376.                                                                     }
  377.                                                                 SampleArray = TempArray;
  378.                                                                 SampleArrayLength = NumSampleFrames;
  379.                                                             }
  380.                                                         /* initialize the array */
  381.                                                         for (Scan = 0; Scan < NumSampleFrames; Scan += 1)
  382.                                                             {
  383.                                                                 PRNGCHK(SampleArray,&(SampleArray[Scan]),
  384.                                                                     sizeof(SampleArray[Scan]));
  385.                                                                 SampleArray[Scan] = 0;
  386.                                                             }
  387.                                                     }
  388.  
  389.                                                 /* perform execution cyle */
  390.                                                 Scan = PlayTrackList;
  391.                                                 Lag = NIL;
  392.                                                 while (Scan != NIL)
  393.                                                     {
  394.                                                         if (!PlayTrackUpdate(Scan->ThisTrack,
  395.                                                             /* this condition is responsible for opening the scanning gap */
  396.                                                             (ScanningGapFrontInEnvelopeTicks >= ScanningGapWidthInEnvelopeTicks),
  397.                                                             NumNoteDurationTicks,NumSampleFrames,SampleArray,
  398.                                                             /* envelope ticks per duration tick */
  399.                                                             (float)(1 / DurationTicksPerEnvelopeClock),
  400.                                                             ScanningGapFrontInEnvelopeTicks))
  401.                                                             {
  402.                                                                 Error = eSynthNoMemory;
  403.                                                                 goto PlaybackFailurePoint;
  404.                                                             }
  405.                                                         if (PlayTrackIsItStillActive(Scan->ThisTrack))
  406.                                                             {
  407.                                                                 Lag = Scan;
  408.                                                                 Scan = Scan->Next;
  409.                                                             }
  410.                                                          else
  411.                                                             {
  412.                                                                 PlayListNodeRec*        Temp;
  413.  
  414.                                                                 if (Lag == NIL)
  415.                                                                     {
  416.                                                                         PlayTrackList = Scan->Next;
  417.                                                                     }
  418.                                                                  else
  419.                                                                     {
  420.                                                                         Lag->Next = Scan->Next;
  421.                                                                     }
  422.                                                                 Temp = Scan;
  423.                                                                 Scan = Scan->Next;
  424.                                                                 DisposePlayTrackInfo(Temp->ThisTrack);
  425.                                                                 ReleasePtr((char*)Temp);
  426.                                                             }
  427.                                                     }
  428.  
  429.                                                 /* keep track of what time it is */
  430.                                                 ScanningGapFrontInEnvelopeTicks += 1;
  431.  
  432.                                                 /* submit the data */
  433.                                                 AbortPlaybackFlag = False;
  434.                                                 if (!(*DataOutCallback)(DataOutRefcon,SampleArray,NumSampleFrames,
  435.                                                     &AbortPlaybackFlag)) /* DataOutRefcon knows about stereo status */
  436.                                                     {
  437.                                                         Error = eSynthDataSubmitError;
  438.                                                         goto PlaybackFailurePoint;
  439.                                                     }
  440.  
  441.                                                 /* and also do the tempo generator */
  442.                                                 OldBeatsPerMinute = CurrentBeatsPerMinute;
  443.                                                 CurrentBeatsPerMinute = TempoControlUpdate(TempoControl,
  444.                                                     NumNoteDurationTicks);
  445.                                                 if (OldBeatsPerMinute != CurrentBeatsPerMinute)
  446.                                                     {
  447.                                                         /* tempo changed, so we have to adjust the clock */
  448.                                                         DurationTicksPerEnvelopeClock = ((LargeBCD2Double(CurrentBeatsPerMinute)
  449.                                                             / (4/*beats per whole note*/ * 60/*seconds per minute*/))
  450.                                                             / EnvelopeRate) * DURATIONUPDATECLOCKRESOLUTION;
  451.                                                     }
  452.                                             }
  453.                                         if (AbortPlaybackFlag)
  454.                                             {
  455.                                                 Error = eSynthUserCancelled;
  456.                                             }
  457.  
  458.                                      PlaybackFailurePoint:
  459.                                         ;
  460.  
  461.                                         /* clean up */
  462.                                         while (PlayTrackList != NIL)
  463.                                             {
  464.                                                 PlayListNodeRec*        Temp;
  465.  
  466.                                                 DisposePlayTrackInfo(PlayTrackList->ThisTrack);
  467.                                                 Temp = PlayTrackList;
  468.                                                 PlayTrackList = PlayTrackList->Next;
  469.                                                 ReleasePtr((char*)Temp);
  470.                                             }
  471.                                     }
  472.  
  473.                                 DisposeTempoControl(TempoControl);
  474.                             }
  475.  
  476.                         /* clean up */
  477.                         ReleasePtr((char*)SampleArray);
  478.                     }
  479.                  else
  480.                     {
  481.                         Error = eSynthNoMemory;
  482.                     }
  483.  
  484.              SkipBuildLoop:
  485.                 ;
  486.             }
  487.          else
  488.             {
  489.                 Error = eSynthPrereqError;
  490.             }
  491.  
  492.         /* clean up */
  493.         FlushLinearTransitionRecords();
  494.         FlushWaveTableOscControl();
  495.         FlushSampleOscControl();
  496.         FlushEvalEnvelopeStateRecords();
  497.         FlushLFOGeneratorRecords();
  498.         FlushModOscControl();
  499.         FlushPlayTrackInfo();
  500.         FlushFrozenNoteStructures();
  501.         FlushCachedOscStateBankRecords();
  502.  
  503.         return Error;
  504.     }
  505.